<?php

namespace Api\Controller;

use Think\Controller;

class IndexController extends PublicController
{
    /**
     * 获取轮播图
     */
    public function getSlideImages()
    {
        $list = M('guanggao')->order('sort = 0, sort asc, id asc')->select();
        foreach ($list as $key => $value) {
            if (!empty($value['photo'])) {
                $list[$key]['photo'] = __DATAURL__ . $value['photo'];
            }
        }
        $this->json_result($list);
    }

    /**
     * 获取活动优选
     */
    public function getYX()
    {
        $list = M('push_index')->order('sort=0, sort asc, id desc')->select();
        $return = [];
        if (!empty($list)) {
            foreach ($list as $index => $row) {
                switch ($row['object_type']) {
                    case 1:
                        $list[$index]['info'] = M("news")->where(['id' => $row['object_id']])->field('title,cover,create_at,zan,comments')->find();

                        break;
                    case 2:
                        $list[$index]['info'] = M("event")->where(['id' => $row['object_id']])->field('title,cover,start_date,end_date,address')->find();
                        break;
                    case 3:
                        $list[$index]['info'] = M("goods")->where(['id' => $row['object_id']])->field('name,cover,point,in_stock,sales_volume')->find();
                        break;
                    case 4:
                        $list[$index]['info'] = M("group")->where(['id' => $row['object_id']])->field('group_type,title,logo,cover,brief')->find();
                        break;
                    case 5:
                        $list[$index]['info'] = M("Inheritance")->where(['id' => $row['object_id']])->field('title,cover,create_at')->find();
                        break;
                }
                $list[$index]['info']['cover'] = __DATAURL__ . $list[$index]['info']['cover'];
                !empty($list[$index]['info']['logo']) && $list[$index]['info']['logo'] = __DATAURL__ . $list[$index]['info']['logo'];
                $return[$row['object_type']][] = $list[$index];
            }
        }

        // 用户登录 + 10积分
        $userId = intval($this->get("id"));
        if ($userId) {
            $today = date('Y-m-d') . ' 00:00:00';
            if (!M('point_log')->where("user_id = {$userId} and type = 2 and create_at > '$today'")->find()) {
                M('user')->where(['id' => $userId])->setInc('point', 10);
                M('point_log')->add(['user_id' => $userId, 'point' => 10, 'type' => 2, 'remark' => '登录获得积分', 'create_at' => date('Y-m-d H:i:s')]);
            }
        }

        $this->json_result($return);
    }

    /**
     * 上传图片接口
     * @author tiedong
     * @date 2018-01-26
     */
    public function uploadFile()
    {
        $file = $_FILES['file'];
        $info = $this->upload_images($file, array('jpg', 'png', 'jpeg', 'gif'), "upload/" . date(Ymd));
        $file_path = 'UploadFiles/' . $info['savepath'] . $info['savename'];
        $ret = array(
            'file_path' => $file_path
        );
        $this->json_result($ret);

    }

}